package com.awspaas.user.apps.cpq.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ibm.db2.jcc.am.id;

public class CPQ_GetModuleNumbersWeb {

	public String getModuleNumbers(UserContext userContext, String bindId, String cs, String gs) {
		XSSFWorkbook workbookwl = new XSSFWorkbook();
		ResponseObject ro = ResponseObject.newOkResponse();
		Map<String, Double> map=new HashMap<>();
		try {
			/**
			 * 获取当前项目的基本参数对应值
			 */
        	  JSONArray mblists=JSONObject.parseArray(cs);
        	  Map<String, JSONObject> pMap = new HashMap<String, JSONObject>();
				Map<String, String> nameMap = new HashMap<String, String>();
				Map<String, String> noMap = new HashMap<>();
        		if(mblists.size()>0) {
					for (int i = 0; i < mblists.size(); i++) {
						JSONObject bo2 = (JSONObject) mblists.get(i);
						String bh =bo2.getString("PARMENTIER_NO");
						String value =  bo2.getString("PARMENTIER_VALUE");
						String name =  bo2.getString("PARMENTIER_NAME");
						String dw =  bo2.getString("DW");
						JSONObject jsonObject=new JSONObject();
						//判断value是否为数据
						if( NumberUtils.isCreatable(value)) {
							jsonObject.put("type", "1");//值为1者表示value为数值
						}else {
							jsonObject.put("type", "0");//值为0者表示value为字符串
						}
						jsonObject.put("value", value);	
						jsonObject.put("PARMENTIER_NO", bh);	
						pMap.put(bh, jsonObject);
						nameMap.put(bh, name);
						int num = i + 1;
						
					}			
				}
        		Sheet sheet = workbookwl.createSheet("Sheet1");
        		JSONArray cslist = new JSONArray();
				for (String key : pMap.keySet()) {
					cslist.add(pMap.get(key));
				}
        		for (int i = 0; i < cslist.size(); i++) {
					int num = i + 1;
					Cell cellB1 = sheet.createRow(i).createCell(1);
					JSONObject jsonObject = (JSONObject) cslist.get(i);
					// BO jsonObject = list.get(i);
					String bh = (String) jsonObject.get("PARMENTIER_NO");
					String type = jsonObject.getString("type");
					if (type.equals("0")) {
						cellB1.setCellValue(jsonObject.getString("value"));
						System.out.println("cellB1:"+bh +":"+ cellB1.getStringCellValue());
					} else {
						String value = jsonObject.getString("value");
						double numValue = Double.parseDouble(value);
						cellB1.setCellValue(numValue);
						System.out.println("cellB1:"+bh +":"+ cellB1.getNumericCellValue());
					}
					noMap.put(bh, "B$" + num);
				}
        		System.out.println("OMAP:"+noMap.toString());
        		if(null!=gs&&!gs.isEmpty()) {
        			Document document = Jsoup.parse(gs);
  				  String newQuation=document.text();
  				 if (!newQuation.equals("0")&&!newQuation.isEmpty()) {
  					System.out.println("参数计算公式："+newQuation);
					// 获取物料数量计算公式中参数代号，如果有则将代号存放在list中
					for (String key : noMap.keySet()) {
						int num = newQuation.indexOf(key);
						if (num > -1) {
							newQuation = newQuation.replace(key, noMap.get(key));
						}
					}
					System.out.println("转换后的参数计算公式："+newQuation);
				
					// 计算出物料数量和当前模块的物料成本价格
					FormulaEvaluator formulaEvaluator = workbookwl.getCreationHelper().createFormulaEvaluator();
					Cell cellB1 = sheet.createRow(cslist.size()+1).createCell(2);
					cellB1.setCellFormula(newQuation);
					formulaEvaluator.evaluateFormulaCell(cellB1);
					// 获取公式计算结果
					CellValue cellValue = formulaEvaluator.evaluate(cellB1);
					System.out.println("The result of the formula is: " + cellValue.getNumberValue());
					
					map.put("num", cellValue.getNumberValue());
					
  				 }
        		}else{
        			map.put("num", 0.0);
        		}
        		ro.setData(map); 
			return ro.toString();
		} catch (Exception e) {
			throw e;
		}
		
	}

}
